package com.evl.wxPayRecord;

import java.text.ParseException;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.evl.common.constant.factory.PageFactory;
import com.evl.common.controller.BaseController;
import com.evl.common.persistence.dao.RefundOrderMapper;
import com.evl.common.persistence.dao.TrainOrderMapper;
import com.evl.common.persistence.dao.WxPayRecordMapper;
import com.evl.common.persistence.model.RefundOrder;
import com.evl.common.persistence.model.TrainOrder;
import com.evl.common.persistence.model.WxPayRecord;
import com.evl.constant.PayStatus;
import com.evl.constant.PayType;
import com.evl.utils.DateUtils;

/**
 * 微信支付日志控制器
 *
 * @author 
 * @Date 2017-07-10 17:46:02
 */
@Controller
@RequestMapping("/wxPayRecord")
public class WxPayRecordController extends BaseController {

    private String PREFIX = "/wxPayRecord/";
    
    @Resource
    WxPayRecordMapper wxPayRecordMapper;
    
    @Resource
    TrainOrderMapper trainOrderMapper;
    
    @Resource
    RefundOrderMapper refundOrderMapper;

    /**
     * 跳转到微信支付日志首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "wxPayRecord.html";
    }

    /**
     * 跳转到添加微信支付日志
     */
    @RequestMapping("/wxPayRecord_add")
    public String wxPayRecordAdd() {
        return PREFIX + "wxPayRecord_add.html";
    }

    /**
     * 跳转到修改微信支付日志
     */
    @RequestMapping("/wxPayRecord_update/{wxPayRecordId}")
    public String wxPayRecordUpdate(@PathVariable Integer wxPayRecordId, Model model) {
        return PREFIX + "wxPayRecord_edit.html";
    }

    /**
     * 获取微信支付日志列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String startTime,@RequestParam(required = false) String endTime) {
    	if(StringUtils.isNotEmpty(startTime)){
    		try {
    			startTime =DateUtils.getFormatDateAndTime(DateUtils.getDateByTimeDD(startTime),"00:00:00");
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	if(StringUtils.isNotEmpty(endTime)){
    		try {
    			endTime =DateUtils.getFormatDateAndTime(DateUtils.getDateByTimeDD(endTime),"23:59:59");
			} catch (ParseException e) {
				e.printStackTrace();
			}
    	}
    	Page<WxPayRecord> page = new PageFactory<WxPayRecord>().defaultPage();
    	List<WxPayRecord> result =  wxPayRecordMapper.selectWxPayRecords(page,startTime,endTime);
    	for (WxPayRecord wxPayRecord : result){
    		if(wxPayRecord.getOrderId()!=null && wxPayRecord.getPayType()!=null){
    			if(wxPayRecord.getPayType().equals(1)){
    				TrainOrder order = trainOrderMapper.selectById(wxPayRecord.getOrderId());
    				if(order !=null){
    					wxPayRecord.setOrderNo(order.getOrderNo());
    				}
    			}else if(wxPayRecord.getPayType().equals(2)){
    				RefundOrder order = refundOrderMapper.selectById(wxPayRecord.getOrderId());
    				if(order !=null){
    					wxPayRecord.setOrderNo(order.getOrderNo());
    				}
    			}
    		}
    		if(wxPayRecord.getPayType()!=null){
    			wxPayRecord.setPayTypeStr(PayType.valueOf(wxPayRecord.getPayType()));
    		}
    		if(wxPayRecord.getPayStatus()!=null){
    			wxPayRecord.setPayStatusStr(PayStatus.valueOf(wxPayRecord.getPayStatus()));
    		}
    	}
    	page.setRecords(result);
    	return super.packForBT(page);
    }

    /**
     * 新增微信支付日志
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除微信支付日志
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改微信支付日志
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 微信支付日志详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
